import { Module } from "vuex"
import { getStorage, setStorage } from '@/utils/storage'
import request from '@/utils/request'
import customRouter from "../../utils/customRouter"
type UserInfo = {
	/* 用户名称 */
	userName : string,
	/* 用户头像 */
	userAvatar : string,
	/* 用户权限 */
	userAuth : 'notLogin' | 'notAuth' | 'success',
	/* 用户所在公司 */
	userCompany : '',
	/* 用户Id */
	Id : number,
	CompanyType : number,
	/* token */
	token : string,
	vip:'VIP1'|'VIP2'|'VIP3'|'',
	/* 运价类型：1官方2最低3预估 */
	RateType:1|2|3,
	/* 按用户1或企业加价2 */
	IsStatus:1|2|0
}
const userInfo : Module<UserInfo, never> = {
	state: {
		userName: '',
		userAvatar: '',
		userAuth: 'notLogin',
		userCompany: '',
		Id: -1,
		CompanyType: -1,
		token: getStorage('token'),
		vip:'',
		RateType:1,
		IsStatus:0
	},
	mutations: {
		/* 修改用户的名称、头像、权限和token */
		editUserInfo(state, info : UserInfo) {
			if (info.userName) {
				state.userName = info.userName
			}
			if (info.userAvatar) {
				state.userAvatar = info.userAvatar
			}
			if (info.userAuth) {
				state.userAuth = info.userAuth
			}
			if (info.userCompany) {
				state.userCompany = info.userCompany
			}
			if (info.Id) {
				state.Id = info.Id
			}
			if (info.CompanyType != undefined) {
				state.CompanyType = info.CompanyType
			}
			if (info.token) {
				state.token = info.token
				setStorage('token', state.token);
			}
			if(info.vip){
				state.vip=info.vip
			}
			if(info.RateType){
				state.RateType=info.RateType
			}
			if(info.IsStatus){
				state.IsStatus=info.IsStatus
			}
		},
		clearUserInfo(state) {
			state.userName = ''
			state.userAvatar = ''
			state.userAuth = 'notLogin'
			state.userCompany = ''
			state.Id = -1
			state.CompanyType = -1
			state.token = ''
			setStorage('token', '')
			customRouter.reLaunch({
				url:'/pages/tabbar/user/user'
			})
		}
	},
	actions: {
		/* 登录方法 */
		login(context, user : { account : string, password : string }) {
			return new Promise((resolve, reject) => {
				request.post('/api/AuthUser/Login', {
					LoginByPassword: true,
					MobilePhone: user.account,
					Password: user.password,
					Vercode: ""
				}).then(res => {
					/* 判断是否登录成功 */
					if (!res.Status) {
						uni.showToast({
							title: res.Message,
							icon: 'none'
						});
						reject('error')
					} else {
						uni.showToast({
							title: res.Message,
						});
						/* 登录成功首先赋值token */
						context.commit('editUserInfo', { token: res.Data })
						/* 然后请求用户信息 */
						context.dispatch('getUserInfo')
						/* 最后抛出promise状态 */
						resolve(res.Data)
					}
				})
			})
		},
		/* 请求/更新用户信息 */
		getUserInfo(context) {
			/* 用户认证（开放接口） */
			request.post('/api/AuthUser/TryWeChatChooseDefaultEmployee').then(resp => {
				/* 获取用户信息(开放接口) */
				request.get('/api/CurrentUser/Current').then(res => {
					context.commit('editUserInfo', { userAvatar: res.Avatar, userName: res.RealName })
					/* 已认证公司的用户 */
					if (resp.Status) {
						request.get('/api/CurrentEmployee/Current').then(res => {
							context.commit('editUserInfo', { userAuth: 'success', userCompany: res.Data.CompanyName, Id: res.Data.Id, userName: res.Data.RealName, CompanyType: res.Data.CompanyType })
						})
						/* 获取公司的vip等级 */
						request.get('/api/CurrentEmployee/GetCurrentEmployeeDetailInfo').then(res=>{
							const current=res.PermissionItems.find(t=>t.ItemKey=='CustomerLevel'&&t.PermissionKey == "FreightRate")
							context.commit('editUserInfo', { vip:current?.Value})
						})
						/* 运价权限获取 */
						request.get('/api/FRRate/GetLogStatus').then(res=>{
							context.commit('editUserInfo', { RateType:res?.Data?.RateType,IsStatus:res?.Data?.IsStatus})
						})
						/* 未认证公司的用户 */
					} else {
						context.commit('editUserInfo', { userAuth: 'notAuth' })
					}
				})
			})
		}
	},
	namespaced: true
}
export default userInfo